<cfcomponent output="false">

	<cfscript>
		variables.instance = StructNew();
		variables.instance.timezones = StructNew();
	</cfscript>
	
	<cffunction name="init" returntype="Timezones" access="public">
		<cfargument name="javaLoader" type="Any" required="true"/>
		<cfargument name="dsn" type="string" required="true"/>
		<cfset variables.javaLoader = arguments.javaLoader/>
		<cfset variables.instance.dsn = arguments.dsn/>
		<cfreturn this/>
	</cffunction>

	<cffunction name="getInstance" returntype="struct" access="public">
		<cfreturn variables.instance />
	</cffunction>

	<cffunction name="get" returntype="Any" access="public">
		<cfargument name="timezone" type="string" hint="The timezone id that ICU4J uses"/>
		<cfif NOT StructKeyExists(variables.instance,arguments.timezone)>
			<cfset variables.instance.timezones[arguments.timezone] = variables.javaLoader.create('net.sanative.kalendar.version_1_0.Timezone').init(arguments.timezone)/>
		</cfif>
		<cfreturn variables.instance.timezones[arguments.timezone] />
	</cffunction>
	
	<cffunction name="getAvailableTimezones" returntype="query" access="public">
		<cfargument name="onlyActive" required="false" type="boolean" default="true"/>
		<cfset var qResults = ""/>
		<cftransaction isolation="read_uncommitted">
			<cfquery name="qResults" datasource="#variables.instance.dsn#">
				SELECT
					Timezones.TimezoneSystemID
					,Timezones.TimezoneDisplay
				FROM dbo.Timezones Timezones
				<cfif arguments.onlyActive>
				WHERE Timezones.IsActive = 1
				</cfif>
			</cfquery>
		</cftransaction>
		<cfreturn qResults/>
	</cffunction>
	
	<cffunction name="castToUTC" access="public" returntype="date">
		<cfargument name="date" required="true" type="date"/>
		<cfargument name="timezoneID" required="true" type="string"/>
		<cfreturn get(arguments.timezoneID).toUTC(Javacast('string',DateFormat(arguments.date,'yyyy-mm-dd') & ' ' & TimeFormat(arguments.date,'HH:mm:ss.l')))/>
	</cffunction>
	
	<cffunction name="castFromUTC" access="public" returntype="date">
		<cfargument name="date" required="true" type="date"/>
		<cfargument name="timezoneID" required="true" type="string"/>
		<cfreturn get(arguments.timezoneID).fromUTC(Javacast('string',DateFormat(arguments.date,'yyyy-mm-dd') & ' ' & TimeFormat(arguments.date,'HH:mm:ss.l')))/>
	</cffunction>
	
</cfcomponent>